package com.gopay.scheduler.service.nucc.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.cust.SMSType;
import com.gopay.common.cps.dao.acct.PayNuccChannelDAO;
import com.gopay.common.cps.dao.acct.PayNuccOptLogDAO;
import com.gopay.common.cps.dao.acct.PayNuccRulesDAO;
import com.gopay.common.domain.bank.route.po.pay.PayNuccChannel;
import com.gopay.common.domain.bank.route.po.pay.PayNuccOptLog;
import com.gopay.common.domain.bank.route.po.pay.PayNuccRules;
import com.gopay.common.domain.log.LogSMSSend;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.dao.sms.LogSMSSendDAO;
import com.gopay.scheduler.service.nucc.AutoSwitchAcctForNuccService;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.constants.SMSResCode;
import com.gopay.sms.common.domain.RequestVO;
import com.gopay.sms.common.domain.ResponseVO;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "autoSwitchAcctForNuccService")
public class AutoSwitchAcctForNuccServiceImpl implements AutoSwitchAcctForNuccService {

	private Logger logger = LoggerFactory.getLogger(AutoSwitchAcctForNuccServiceImpl.class);

	@Resource(name = "commonQueryDAO")
	private CommonQueryDAO commonQueryDAO;

	@Resource(name = "payNuccChannelDAO")
	private PayNuccChannelDAO payNuccChannelDAO;

	@Resource(name = "payNuccRulesDAO")
	private PayNuccRulesDAO payNuccRulesDAO;

	@Resource(name = "payNuccOptLogDAO")
	private PayNuccOptLogDAO payNuccOptLogDAO;

	@Resource(name = "logSMSSendDAO")
	private LogSMSSendDAO logSMSSendDAO;

	@Resource(name = "smsNuccConfig")
	private Map<String, String> smsNuccConfig;

	@Override
	public List<PayNuccChannel> findPayNuccChannelByType(String type) {
		return payNuccChannelDAO.findPayNuccChannelByType(type);
	}

	@Override
	public List<PayNuccRules> findPayNuccRulesByChannId(String channelId) {
		return payNuccRulesDAO.findPayNuccRulesByChannId(channelId);
	}

	@Override
	public void save(PayNuccOptLog payNuccOptLog) {
		payNuccOptLogDAO.save(payNuccOptLog);
	}

	@Override
	public List<PayNuccRules> findPayNuccRules(long channelId, String whFlag, String curDate, BigDecimal bankAcctAmt) {
		return payNuccRulesDAO.findPayNuccRulesByChannId(channelId, whFlag, curDate, bankAcctAmt);
	}

	@Override
	public PayNuccChannel findPayNuccChannelByRecBankCode(String recBankCode) {
		return payNuccChannelDAO.findPayNuccChannelByRecBankCode(recBankCode);
	}

	@Override
	public void update(PayNuccChannel payNuccChannel) {

		payNuccChannelDAO.update(payNuccChannel);
	}

	@Override
	public PayNuccRules findPayNuccRulesByChannIdAndRollFlag(long channelId, String rollBackFlag) {

		return payNuccRulesDAO.findPayNuccRulesByChannIdAndRollFlag(channelId, rollBackFlag);
	}

	@Override
	public void sendAutoSwitchSMS(PayNuccChannel payNuccChannel, PayNuccRules payNuccRule) {
		String content = "收款银行为" + payNuccChannel.getRecBankName() + "的网联通道出款，因为“" + payNuccRule.getRuleName() + "”已切换至" +payNuccChannel.getCurBankName()+"****"+payNuccChannel.getCurBankAcct().substring(payNuccChannel.getCurBankAcct().length()-4)+"账户";

		this.sendSMS(smsNuccConfig.get("mobile"), SMSType.OTHER_SMS, SMSAppId._90000, content);
	}
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	@Override
	public void updatePayNuccChannel(PayNuccChannel payNuccChannel, PayNuccOptLog optLog) {
		logger.info("-----------AutoSwitchAcctForNuccJob updatePayNuccChannel start-----------");
		payNuccOptLogDAO.save(optLog);
		payNuccChannelDAO.update(payNuccChannel);
		logger.info("-----------AutoSwitchAcctForNuccJob updatePayNuccChannel end-----------");
	}

	private int sendSMS(String mobileNo, SMSType smsType, SMSAppId appid, String content) {
		// 调用短信网关
		RequestVO req = new RequestVO();
		req.setMobile(mobileNo);
		req.setAppId(appid.value);
		req.setContent(content);
		ResponseVO resposeVo = GopaySMSHttpClient.getInstance().createVerificationCode(req);
		if (resposeVo.getStatusCode().equals(SMSResCode.SMS_DEND_SUCCESS.value)) {
			logSMSSendDAO.save(buildLog(mobileNo, smsType));
			logger.info("短信返回Msg = " + resposeVo.getMessage() + "短信返回code = " + resposeVo.getStatusCode());
			return 1;
		} else {
			logger.info("短信返回Msg = " + resposeVo.getMessage() + "短信返回code = " + resposeVo.getStatusCode());
			return 2;
		}
	}

	private LogSMSSend buildLog(String mobileNo, SMSType smsType) {
		LogSMSSend log = new LogSMSSend();
		Date date = new Date();
		log.setMobileNum(mobileNo);
		log.setMobileID(smsType.value);
		log.setSendTime(date);
		log.setLastUpdTs(date);
		log.setCreatedTs(date);
		return log;
	}

}
